//
//  FeelingCurveTableViewCell.m
//  RTUApp
//
//  Created by 陈德军 on 2017/6/20.
//  Copyright © 2017年 陈德军. All rights reserved.
//

#import "FeelingCurveTableViewCell.h"
// ID（1byte）+截距（4byte）+B1（4byte）+B2（4byte）+B3（4byte）+干湿度（4byte）+标志（1byte）+ 符号（10byte）


@interface FeelingCurveTableViewCell()<UITextFieldDelegate>{
    
}


@end

@implementation FeelingCurveTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+(instancetype)cellWithtableView:(UITableView *)tableview
{
    static NSString *ID = @"cell";
    FeelingCurveTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if(!cell)
    {
        cell = [[FeelingCurveTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
    
}

//重写布局
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.IDLabel = [UILabel new];
        self.IDLabel.textAlignment = NSTextAlignmentCenter;
        self.IDLabel.textAlignment = NSTextAlignmentCenter;
        self.IDLabel.size = CGSizeMake(90, 23);
        self.IDLabel.left = 0;
        self.IDLabel.top = 0;
        [self.contentView addSubview:self.IDLabel];
        
        self.interceptTF = [UITextField new];
        self.interceptTF.textAlignment = NSTextAlignmentCenter;
        self.interceptTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.interceptTF.delegate = self;
        self.interceptTF.size = CGSizeMake(90, 23);
        self.interceptTF.left = self.IDLabel.right;
        self.interceptTF.top = self.IDLabel.top;
        [self.contentView addSubview:self.interceptTF];
        
        self.B1TF = [UITextField new];
        self.B1TF.textAlignment = NSTextAlignmentCenter;
        self.B1TF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.B1TF.delegate = self;
        self.B1TF.size = CGSizeMake(90, 23);
        self.B1TF.left = self.interceptTF.right;
        self.B1TF.top = self.IDLabel.top;
        [self.contentView addSubview:self.B1TF];
        
        self.B2TF = [UITextField new];
        self.B2TF.textAlignment = NSTextAlignmentCenter;
        self.B2TF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.B2TF.delegate = self;
        self.B2TF.size = CGSizeMake(90, 23);
        self.B2TF.left = self.B1TF.right;
        self.B2TF.top = self.IDLabel.top;
        [self.contentView addSubview:self.B2TF];
        
        self.B3TF = [UITextField new];
        self.B3TF.textAlignment = NSTextAlignmentCenter;
        self.B3TF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.B3TF.delegate = self;
        self.B3TF.size = CGSizeMake(90, 23);
        self.B3TF.left = self.B2TF.right;
        self.B3TF.top = self.IDLabel.top;
        [self.contentView addSubview:self.B3TF];
        
        self.dryHumidityTF = [UITextField new];
        self.dryHumidityTF.textAlignment = NSTextAlignmentCenter;
        self.dryHumidityTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.dryHumidityTF.delegate = self;
        self.dryHumidityTF.size = CGSizeMake(90, 23);
        self.dryHumidityTF.left = self.B3TF.right;
        self.dryHumidityTF.top = self.IDLabel.top;
        [self.contentView addSubview:self.dryHumidityTF];
        
        self.markTF = [UILabel new];
        self.markTF.textAlignment = NSTextAlignmentCenter;
        self.markTF.userInteractionEnabled = YES;
        UITapGestureRecognizer *markTRG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickMarkLabelEvent)];
        [self.markTF addGestureRecognizer:markTRG];
        self.markTF.size = CGSizeMake(90, 23);
        self.markTF.left = self.dryHumidityTF.right;
        self.markTF.top = self.IDLabel.top;
        [self.contentView addSubview:self.markTF];
        
        self.symbolTF = [UITextField new];
        self.symbolTF.textAlignment = NSTextAlignmentCenter;
        self.symbolTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.symbolTF.delegate = self;
        self.symbolTF.size = CGSizeMake(90, 23);
        self.symbolTF.left = self.markTF.right;
        self.symbolTF.top = self.IDLabel.top;
        [self.contentView addSubview:self.symbolTF];
        
        

    }
    return self;
}

//给cell控件赋值
-(void)setModel:(FeelingCurveModel *)model{
    self.IDLabel.text = model.IDStr;
    self.interceptTF.text = model.interceptStr;
    self.B1TF.text = model.B1Str;
    self.B2TF.text = model.B2Str;
    self.B3TF.text = model.B3Str;
    self.dryHumidityTF.text = model.dryHumidityStr;
    if ([model.markStr isEqualToString:@"0"]) {
        self.markTF.text = @"质量含水率";
    }else{
        self.markTF.text = @"体积含水率";
    }
    self.symbolTF.text = model.symbolStr;
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField isEqual:self.interceptTF]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickInterceptEvent:andValue:)]) {
            [self.delegate delegateClickInterceptEvent:self.IDLabel.text andValue:self.interceptTF.text];
        }
    }else if ([textField isEqual:self.B1TF]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickB1Event:andValue:)]) {
            [self.delegate delegateClickB1Event:self.IDLabel.text andValue:self.B1TF.text];
        }
    }else if ([textField isEqual:self.B2TF]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickB2Event:andValue:)]) {
            [self.delegate delegateClickB2Event:self.IDLabel.text andValue:self.B2TF.text];
        }
    }else if ([textField isEqual:self.B3TF]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickB3Event:andValue:)]) {
            [self.delegate delegateClickB3Event:self.IDLabel.text andValue:self.B3TF.text];
        }
    }else if ([textField isEqual:self.dryHumidityTF]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickInterceptEvent:andValue:)]) {
            [self.delegate delegateClickInterceptEvent:self.IDLabel.text andValue:self.interceptTF.text];
        }
    }else if ([textField isEqual:self.symbolTF]){
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickSymbolEvent:andValue:)]) {
            [self.delegate delegateClickSymbolEvent:self.IDLabel.text andValue:self.symbolTF.text];
        }
    }else {
        NSLog(@"出来了");
    }
}

#pragma mark - 按钮点击事件，通过代理模式响应
-(void)btnClick:(UIButton *)btn
{
    [self.delegate didClickButton:btn];
}


- (void)clickMarkLabelEvent{
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegateClickMarkEvent:)]) {
        [self.delegate delegateClickMarkEvent:self.IDLabel.text];
    }
}

@end
